from app import db
from flask import jsonify
from ..domain.roomStatus import RoomStatus


def get_roomstatus_by_roomid(roomid):
    try:
        room_status = RoomStatus.query.filter(RoomStatus.roomid == roomid)
        return room_status
    except Exception as e:
        print(e)



def room_status_update(room_status):
    _room_status = RoomStatus.query.filter(RoomStatus.roomid == room_status.roomid).first()
    _room_status.temperature = room_status.temperature
    _room_status.moisture = room_status.moisture
    _room_status.door = room_status.door
    _room_status.electricity = room_status.electricity
    db.session.commit()

    if (int(_room_status.temperature) > 50):
        return jsonify(
            {'valid': True, 'room_status': {"roomid": _room_status.roomid, "temperature": _room_status.temperature,
                                            "moisture": _room_status.moisture,
                                            "door": _room_status.door, "electricity": _room_status.electricity
                                            }, 'code': 201})
    if (int(_room_status.moisture) > 90):
        return jsonify(
            {'valid': True, 'room_status': {"roomid": _room_status.roomid, "temperature": _room_status.temperature,
                                            "moisture": _room_status.moisture,
                                            "door": _room_status.door, "electricity": _room_status.electricity
                                            }, 'code': 202})
    if (int(_room_status.door) != 0):
        return jsonify(
            {'valid': True, 'room_status': {"roomid": _room_status.roomid, "temperature": _room_status.temperature,
                                            "moisture": _room_status.moisture,
                                            "door": _room_status.door, "electricity": _room_status.electricity
                                            }, 'code': 203})
    if (int(_room_status.electricity) != 0):
        return jsonify(
            {'valid': True, 'room_status': {"roomid": _room_status.roomid, "temperature": _room_status.temperature,
                                            "moisture": _room_status.moisture,
                                            "door": _room_status.door, "electricity": _room_status.electricity
                                            }, 'code': 204})
    return jsonify(
        {'valid': True, 'room_status': {"roomid": _room_status.roomid, "temperature": _room_status.temperature,
                                        "moisture": _room_status.moisture,
                                        "door": _room_status.door, "electricity": _room_status.electricity
                                        }, 'code': 200})


def get_video_type(room_status):
    if (int(room_status.temperature) > 50):
        return "高温异常"
    if (int(room_status.moisture) > 90):
        return "湿度异常"
    if (int(room_status.door) != 0):
        return "门未关"
    if (int(room_status.electricity) != 0):
        return "未断电"
